package org.sny7.openOfflineList.bean;

import java.util.List;

import org.dom4j.Node;

public class Images {
	
	public static Image[] image;
	
	public static boolean exists;
	
	public static int width;
	
	public static int height;
	
	private static Images instance = new Images();
	
	private Images() {
		
	}
	
	public static Images getInstance() {
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public void generate(Node imagesNode) {
		if (imagesNode == null) {
			exists = false;
			return;
		}
		exists = true;
		width = Integer.parseInt(imagesNode.selectSingleNode("@width").getText());
		height = Integer.parseInt(imagesNode.selectSingleNode("@height").getText());
		generateImages(imagesNode.selectNodes("image"));
	}
	
	private void generateImages(List<Node> imageList) {
		image = new Image[imageList.size()];
		int i = 0;
		for (Node imageNode : imageList) {
			Image img = new Image();
			img.setX(imageNode.selectSingleNode("@x").getText());
			img.setY(imageNode.selectSingleNode("@y").getText());
			img.setWidth(imageNode.selectSingleNode("@width").getText());
			img.setHeight(imageNode.selectSingleNode("@height").getText());
			image[i++] = img;
		}
	}

}
